package com.sas.hnn;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
	  /**
	 * @uml.property  name="tAG"
	 */
	private final String TAG=MyService.class.getSimpleName();
	  /**
	 * @uml.property  name="mServiceLooper"
	 * @uml.associationEnd  
	 */
	private Looper mServiceLooper;
	  /**
	 * @uml.property  name="mServiceHandler"
	 * @uml.associationEnd  inverse="this$0:com.sas.hnn.MyService$ServiceHandler"
	 */
	private ServiceHandler mServiceHandler;

	  // Handler that receives messages from the thread
	  private final class ServiceHandler extends Handler {
		  private Service service;
		  
	      public ServiceHandler(Looper looper, Service service) {
	          super(looper);
	          this.service= service;
	      }
	      @Override
	      public void handleMessage(Message msg) {
	          // Normally we would do some work here, like download a file.
	          // For our sample, we just sleep for 5 seconds.
	          long endTime = System.currentTimeMillis() + 5*1000;
	          while (System.currentTimeMillis() < endTime) {
	              synchronized (this) {
	                  try {
	                      wait(endTime - System.currentTimeMillis());
	                  } catch (Exception e) {
	                  }
	              }
	          }
	          ((MyService) service).successToast();
	          // Stop the service using the startId, so that we don't stop
	          // the service in the middle of handling another job
	          stopSelf(msg.arg1);
	      }
	  }

	  @Override
	  public void onCreate() {
	    // Start up the thread running the service.  Note that we create a
	    // separate thread because the service normally runs in the process's
	    // main thread, which we don't want to block.  We also make it
	    // background priority so CPU-intensive work will not disrupt our UI.
	    HandlerThread thread = new HandlerThread("ServiceStartArguments");
	    thread.start();
	    
	    // Get the HandlerThread's Looper and use it for our Handler 
	    mServiceLooper = thread.getLooper();
	    mServiceHandler = new ServiceHandler(mServiceLooper,this);
	  }

	  @Override
	  public int onStartCommand(Intent intent, int flags, int startId) {
		  Log.d(TAG,"in onStartCommand()");
		  Toast.makeText(this, "extra Thread service starting", Toast.LENGTH_SHORT).show(); 

	      Message msg = mServiceHandler.obtainMessage();
	      msg.arg1 = startId;
	      mServiceHandler.sendMessage(msg);   
	      // If we get killed, after returning from here, restart
	      return START_STICKY;
	  }

	  @Override
	  public IBinder onBind(Intent intent) {
	      // We don't provide binding, so return null
	      return null;
	  }
	  
	  @Override
	  public void onDestroy() {
		Log.d(TAG,"in onDestroy()");
	    
	  }
	  
	  public void successToast() {
		  Toast.makeText(this, "extra Thread service done", Toast.LENGTH_SHORT).show();
	  }
	}